local shehu = fk.CreateSkill{
  name = "dajubei__shehu",
  tags = {"Turn"},
}

Fk:loadTranslationTable{
  ["dajubei__shehu"] = "慑胡",
  [":dajubei__shehu"] = "回合技，当你使用【杀】指定一名角色为目标后，你可以亮出牌堆顶的X张牌（X为你与该角色的距离）。"
  .."若此【杀】与亮出牌的花色均不同，则不可被响应；此【杀】造成伤害后，你获得亮出牌中花色不重复的牌。",

  ["#dajubei__shehu-invoke"] = "慑胡：是否对 %dest 发动“慑胡”？",
  ["#dajubei__shehu_delay"] = "慑胡",
  ["#shehuPoxi-prompt"] = "慑胡：选择任意数量花色各不同的牌",

  ["shehuPoxi"] = "慑胡",

  ["$dajubei__shehu1"] = "饮羽没石，虏骑慑服。",
  ["$dajubei__shehu2"] = "惮像蛮胡，驰射难中。",
}

Fk:addPoxiMethod{
  name = "shehuPoxi",
  prompt = "#shehuPoxi-prompt",
  card_filter = function(to_select, selected, data)
    local s = Fk:getCardById(to_select).suit
    for _, id in ipairs(selected) do
      if Fk:getCardById(id).suit == s then return false end
    end
    return true
  end,
  feasible = function(selected, data)
    return #selected >= 0
  end,
}

shehu:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.trueName == "slash" and
    player:usedSkillTimes(shehu.name) == 0 and not data.to.dead
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = shehu.name,
      prompt = "#dajubei__shehu-invoke::"..data.to.id,
    })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = data.to
    local cards = room:getNCards(player:distanceTo(to))
    data.extra_data = data.extra_data or {}
    data.extra_data.dajubei__shehu = {player.id, cards}
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, shehu.name, nil, true, player)
    if table.every(cards, function (id)
      return data.card.suit ~= Fk:getCardById(id).suit
    end) then
      data.use.disresponsiveList = data.use.disresponsiveList or {}
      table.insert(data.use.disresponsiveList, data.to)
    end
  end,
})

shehu:addEffect(fk.Damage, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(shehu.name) and data.card and data.card.trueName == "slash" then
      local use = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if use and use.data.extra_data then
        local d = use.data.extra_data.dajubei__shehu ---@type table<integer, integer[]>
        if d and d[1] == player.id then
          event:setCostData(self, d[2])
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self) ---@type integer[]
    local room = player.room
    cards = table.filter(cards, function (id)
      return room:getCardArea(id) == Card.Processing
    end)
    cards = room:askToPoxi(player, {
      poxi_type = "shehuPoxi",
      cancelable = false,
      data = {{"dajubei__shehu", cards}},
      extra_data = {
        min = 1,
        max = 3,
        skillName = "dajubei__shehu",
      }
    })
    if #cards > 0 then
      room:obtainCard(player, cards, true, fk.ReasonJustMove, player, shehu.name)
    end
  end
})

shehu:addEffect(fk.CardUseFinished, {
  can_refresh = function (self, event, target, player, data)
    return data.extra_data and data.extra_data.dajubei__shehu
  end,
  on_refresh = function (self, event, target, player, data)
    local d = data.extra_data.dajubei__shehu
    local room = player.room
    local cards = table.filter(d[2], function (id) return room:getCardArea(id) == Card.Processing end)
    if #cards > 0 then
      room:cleanProcessingArea(cards)
    end
  end,
})

return shehu